﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HVAC.AuxiliaryCurve
{
	public class RefPlaneDraw : AuxiliaryDraw
	{
		public RefPlaneDraw(ExternalCommandData amdArgu, Dictionary<XYZ, XYZ> dicXArgu, Dictionary<XYZ, XYZ> dicYArgu, ref ICollection<ElementId> idsArgu) : base(amdArgu, dicXArgu, dicYArgu, ref idsArgu)
		{
		}

		public override bool DrawCurve()
		{
			if (!base.IsInTolerance())
			{
				return false;
			}
			if (this.dicX.Count < 1 && this.dicY.Count < 1)
			{
				return false;
			}
			foreach (KeyValuePair<XYZ, XYZ> keyValuePair in this.dicX)
			{
				ReferencePlane referencePlane = RefPlaneDraw.DrawRefPlane(this.m_CmdData, keyValuePair.Key, keyValuePair.Value);
				this.ids.Add(referencePlane.Id);
			}
			foreach (KeyValuePair<XYZ, XYZ> keyValuePair2 in this.dicY)
			{
				ReferencePlane referencePlane2 = RefPlaneDraw.DrawRefPlane(this.m_CmdData, keyValuePair2.Key, keyValuePair2.Value);
				this.ids.Add(referencePlane2.Id);
			}
			return true;
		}

		public static ReferencePlane DrawRefPlane(ExternalCommandData commandData, XYZ bud, XYZ end)
		{
			ItemFactoryBase create = commandData.Application.ActiveUIDocument.Document.Create;
            Autodesk.Revit.DB.Document document = commandData.Application.ActiveUIDocument.Document;
			Autodesk.Revit.DB.View activeView = document.ActiveView;
			Level level = null;
			if (activeView is ViewPlan)
			{
				level = activeView.GenLevel;
			}
			if (level == null)
			{
				throw new InvalidOperationException();
			}
			double num = level.Elevation - 15.0;
			XYZ xyz = new XYZ(bud.X, bud.Y, num);
			XYZ xyz2 = new XYZ(end.X, end.Y, num);
			XYZ xyz3 = new XYZ(0.0, 0.0, 1.0);
			ReferencePlane referencePlane = create.NewReferencePlane(xyz, xyz2, xyz3, document.ActiveView);
			ElementSet elementSet = new ElementSet();
			elementSet.Insert(referencePlane);
			if (referencePlane.IsHidden(document.ActiveView))
			{
				document.ActiveView.UnHideYJKElements(elementSet);
			}
			return referencePlane;
		}
	}
}
